using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Infrastructure.AliyunOSS
{
    /// <summary>
    /// 阿里云OSS服务扩展类
    /// </summary>
    public static class AliyunOSSServiceExtensions
    {
        /// <summary>
        /// 添加阿里云OSS服务
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="configuration">配置</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddAliyunOSSService(this IServiceCollection services, IConfiguration configuration)
        {
            // 从配置中读取阿里云OSS配置
            var ossSection = configuration.GetSection("OSSType:AliyunOSS");
            
            // 配置阿里云OSS选项
            services.Configure<AliyunOSSOptions>(options =>
            {
                options.Endpoint = ossSection["Endpoint"] ?? string.Empty;
                options.AccessKeyId = ossSection["AccessKeyId"] ?? string.Empty;
                options.AccessKeySecret = ossSection["AccessKeySecret"] ?? string.Empty;
                options.BucketName = ossSection["BucketName"] ?? "rabbit-traceability";
                options.Domain = ossSection["Domain"];
                options.UseHttps = bool.TryParse(ossSection["UseHttps"], out bool useHttps) && useHttps;
            });
            
            // 注册阿里云OSS服务
            services.AddSingleton<IAliyunOSSService, AliyunOSSService>();
            
            return services;
        }
        
        /// <summary>
        /// 添加阿里云OSS服务（使用自定义配置）
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="configureOptions">配置选项</param>
        /// <returns>服务集合</returns>
        public static IServiceCollection AddAliyunOSSService(this IServiceCollection services, Action<AliyunOSSOptions> configureOptions)
        {
            // 配置阿里云OSS选项
            services.Configure(configureOptions);
            
            // 注册阿里云OSS服务
            services.AddSingleton<IAliyunOSSService, AliyunOSSService>();
            
            return services;
        }
    }
} 